package cat.tom.service.impl;

import cat.tom.utils.JwtHelper;
import cat.tom.utils.MD5Util;
import cat.tom.utils.Result;
import cat.tom.utils.ResultCodeEnum;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cat.tom.pojo.User;
import cat.tom.service.UserService;
import cat.tom.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author llllle
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-03-08 01:03:36
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result login(User user) {

        // 根据账号查询
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User loginUser = userMapper.selectOne(queryWrapper);

        // 账号判断
        if(loginUser==null){
            // 账号错误
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }

        // 密码判断
        if (!StringUtils.isEmpty(user.getUserPwd()) &&
        loginUser.getUserPwd().equals(MD5Util.encrypt(user.getUserPwd()))){

            // 密码正确 根据用户唯一标识生成token
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            Map data = new HashMap();
            data.put("token", token);
            return Result.ok(data);
        }
        return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);


    }

    @Override
    public Result getUserInfo(String token) {

        // token 过期返回未登录
        if (jwtHelper.isExpiration(token)){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }

        // 获取UserId
        int userId = jwtHelper.getUserId(token).intValue();

        // 获取用户信息
        User user = userMapper.selectById(userId);
        if (user!=null){
            user.setUserPwd(null);
            Map data = new HashMap();
            data.put("loginUser", user);
            return Result.ok(data);
        }

        return Result.build(null, ResultCodeEnum.NOTLOGIN);

    }

    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        if (user!=null){
            return Result.build(null,ResultCodeEnum.USERNAME_ERROR);
        }
        return Result.ok(null);
    }

    @Override
    public Result regist(User user) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getUsername, user.getUsername());
        Long count = userMapper.selectCount(qw);

        if (count>0) return Result.build(null, ResultCodeEnum.USERNAME_ERROR);

        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        int rows = userMapper.insert(user);
        System.out.println("rows = " + rows);
        return Result.ok(null);
    }
}




